package com.pay.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pay.manager.dao.ChannelMapper;

import com.pay.manager.dto.ChannelDTO;


import com.pay.manager.pojo.Channel;
import com.pay.manager.pojo.Merchant;
import com.pay.manager.response.PageDataResult;
import com.pay.manager.service.ChannelService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ChannelServiceImpl
 * @Description:
 * @author: yjw
 * @version: 1.0
 * @date: 2020/6/12 11:04
 */
@Service
public class ChannelServiceImpl implements ChannelService {
	
	@Autowired
	private ChannelMapper channelDao;

	@Override
	public PageDataResult getChannelList(ChannelDTO channelSearch, Integer pageNum, Integer pageSize) {
		 PageDataResult pageDataResult = new PageDataResult();
	        List<Channel> channel = channelDao.getChannelList(channelSearch);
	        PageHelper.startPage(pageNum, pageSize);
	        if(channel.size() != 0){
	            PageInfo<Channel> pageInfo = new PageInfo<>(channel);
	            pageDataResult.setList(channel);
	            pageDataResult.setTotals((int) pageInfo.getTotal());
	        }

	        return pageDataResult;
	}

	@Override
	public Map<String, Object> lower(Integer cid, String operationPwd) {
		Map<String,Object> data = new HashMap();
		if(operationPwd.equals("123456")) {
			Channel channel = channelDao.selectById(cid);
			channel.setState("0");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			channel.setUpdateTime(sdf.format(new Date()));
			channelDao.updateById(channel);
			data.put("code",1);
			data.put("msg","渠道已停止使用！");
		}else {
			data.put("code",0);
			data.put("msg","操作密码有误！");
			return data;
		}

		return data;
	}

	@Override
	public Map<String, Object> applyPass(Integer cid, String operationPwd) {
		Map<String,Object> data = new HashMap();
		if(operationPwd.equals("123456")) {
			Channel channel = channelDao.selectById(cid);
			channel.setState("1");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			channel.setUpdateTime(sdf.format(new Date()));
			channelDao.updateById(channel);
			data.put("code",1);
			data.put("msg","渠道已开启使用！");
		}else {
			data.put("code",0);
			data.put("msg","操作密码有误！");
			return data;
		}

		return data;
	}

	@Override
	public Map<String, Object> addChannel(String channelId, String channelName, String channelMchId, String mchId, String param, String remark) {
		Map<String,Object> data = new HashMap();

		Channel channel = new Channel();
		channel.setChannelId(channelId);
		channel.setChannelName(channelName);
		channel.setChannelMchId(channelMchId);
		channel.setMchId(mchId);
		channel.setParam(param);
		channel.setRemark(remark);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		channel.setCreateTime(sdf.format(new Date()));
		channel.setUpdateTime(sdf.format(new Date()));
		channelDao.insert(channel);
		data.put("code",1);
		data.put("msg","渠道创建成功！");
		return data;
	}


}
